package com.ssyt.ExaminationSystem.admin.dao;

import java.util.List;

import com.ssyt.ExaminationSystem.admin.entity.PermissionEntity;
import com.ssyt.ExaminationSystem.admin.entity.RoleEntity;
import com.ssyt.ExaminationSystem.student.entity.GradeEntity;
import com.sunt.util.DBUtil;

/**
 * 
 * @author Administrator
 *
 */
public class SystemDaoImpl implements IsystemDao {

	/**
	 * 初始化所有的角色
	 */
	public List<RoleEntity> initRoles() {
		String sql = "Select * from tb_role ";
		return DBUtil.executeQuery(RoleEntity.class, sql);
	}

	/**
	 * 通过账号获取所有的权限菜单
	 */
	@Override
	public List<PermissionEntity> getMenusByAccount(String account) {
		String sql = "SELECT *from tb_permission WHERE permission_id in ( SELECT permission_id  from tb_relation WHERE role_id=(select role_id from tb_user where account=?))";
		return DBUtil.executeQuery(PermissionEntity.class, sql, account);
	}

	/**
	 * 查询所有的角色菜单
	 */
	@Override
	public List<PermissionEntity> getPermissions() {
		String sql = "select * from tb_permission";
		return DBUtil.executeQuery(PermissionEntity.class, sql);
	}

	@Override
	public List<PermissionEntity> getGrantsByRoleId(String roleId) {
		String sql = "SELECT *from tb_permission WHERE permission_id in ( SELECT permission_id  from tb_relation WHERE role_id=?)";
		return DBUtil.executeQuery(PermissionEntity.class, sql, roleId);
	}

	/**
	 * 增加角色权限，此方法以及下面的两个方法都是为业务层的是否授权的方法（ isDoGrant）服务的
	 */
	@Override
	public int addGrant(String permissionId, String roleId) {
		String sql = "insert into tb_relation values(null,?,?)";
		return DBUtil.executeUpdate(sql, roleId, permissionId);
	}

	/**
	 * 根据角色编号查询该角色时候已经拥有了权限
	 */
	@Override
	public boolean isGrant(String roleId) {
		String sql = "select 1 from tb_relation where role_id =?";
		List<PermissionEntity> list = DBUtil.executeQuery(
				PermissionEntity.class, sql, roleId);

		return list != null && list.size() > 0 ? true : false;
	}

	/**
	 * 根据角色编号取消该角色所有的权限
	 */
	@Override
	public int revoke(String roleId) {
		String sql = "delete from tb_relation where role_id=? ";
		return DBUtil.executeUpdate(sql, roleId);
	}

	/**
	 * 增加角色
	 */
	@Override
	public int addRole(String roleName) {
		String sql = "insert into tb_role values(null,?)";
		return DBUtil.executeUpdate(sql, roleName);
	}

	/**
	 * 删除角色
	 */
	@Override
	public int deleteRole(String roleId) {

		String sql = "delete from tb_role where role_id =?";
		return DBUtil.executeUpdate(sql, roleId);
	}

	/**
	 * 班级管理
	 */
	@Override
	public List<GradeEntity> classManager() {
		String sql = "select * from tb_grade";
		return DBUtil.executeQuery(GradeEntity.class, sql);
	}

	/**
	 * 删除班级
	 */
	@Override
	public int deleteGrade(String gradeId) {
		String sql = "delete from tb_grade where grade_id =?";
		return DBUtil.executeUpdate(sql, gradeId);
	}

	/**
	 * 修改前的查询
	 */
	@Override
	public GradeEntity preUpdateGrade(String gradeId) {
		String sql = "select * from tb_grade where grade_id=?";
		List<GradeEntity> list = DBUtil.executeQuery(GradeEntity.class, sql,
				gradeId);
		return list != null && list.size() > 0 ? list.get(0) : null;
	}

	/**
	 * 修改班级
	 */
	@Override
	public int updateGrade(String gradeId, String gradeName) {
		int row = 0;
		String sql = "update tb_grade set grade_name=? where grade_id=?";
		row = DBUtil.executeUpdate(sql, gradeName, gradeId);
		return row;
	}

	/**
	 * 查询所有的赋权限
	 */
	@Override
	public List<PermissionEntity> getFatherGrant() {
		String sql = "SELECT permission_id,permission_name from tb_permission WHERE parent_id ='root';";
		List<PermissionEntity> list = DBUtil.executeQuery(
				PermissionEntity.class, sql);

		return list;
	}

	/**
	 * 通过付权限查询其下面的子权限
	 */
	@Override
	public List<PermissionEntity> getSonGrantsById(String fatherId) {
		String sql = "SELECT permission_id from tb_permission where parent_id =?;";
		return DBUtil.executeQuery(PermissionEntity.class, sql, fatherId);
	}

	/**
	 * 从已有付权限的情况下，增加子权限
	 */
	@Override
	public int addPermissions(PermissionEntity per) {
		String sql = "insert into tb_permission values(?,?,?,?)";
		Object[] parameters = { per.getPermissionId(), per.getParentId(),
				per.getPermissionName(), per.getPermissionUrl() };
		return DBUtil.executeUpdate(sql, parameters);
	}

	/**
	 * 查询所有的权限
	 */
	@Override
	public List<PermissionEntity> getAllGrants() {
		String sql = "select * from tb_permission";
		return DBUtil.executeQuery(PermissionEntity.class, sql);
	}

	/**
	 * 删除权限
	 */
	@Override
	public int deleteGrant(String permissionId) {
		String sql = "delete from tb_permission where permission_id=?";
		return DBUtil.executeUpdate(sql, permissionId);
	}

	/**
	 * 修改权限之前的查询
	 */
	@Override
	public PermissionEntity preUpdateGrant(String permissionId) {
		String sql = "select * from tb_permission where permission_id =?";
		return DBUtil.executeQuery(PermissionEntity.class, sql, permissionId)
				.get(0);
	}

	@Override
	public int updateGrant(PermissionEntity permission) {
		String sql = "update tb_permission set parent_id=?,permission_name=?,permission_url=? where permission_id=?";
		Object[] parameters = { permission.getParentId(),
				permission.getPermissionName(), permission.getPermissionUrl(),
				permission.getPermissionId() };
		return DBUtil.executeUpdate(sql, parameters);

	}

}
